#include "./mq_connection.hpp"
#include "../mqcommon/mq_loger.hpp"

void cb(const mq::Channel::ptr& channel,const std::string& consumer_tag ,const mq::BasicProperties* bp,const std::string& body)
{
    std::cout << consumer_tag << "消费了消息：" << body << std::endl;
    channel->basicAck(bp->id());
}

int main(int argc,char* argv[]){
    //0. 判断个数是否正确
    if(argc != 2){
        DLOG("usage：./consume_client queue1");
        return -1;
    }

    //1. 创建 连接对象
        //创建异步线程给到连接
    mq::AsyncWorker::ptr awp = std::make_shared<mq::AsyncWorker>();

    mq::Connection::ptr conn = std::make_shared<mq::Connection>("127.0.0.1",8085,awp);

    //2. 通过连接，创建信道对象
    mq::Channel::ptr channel =  conn->openChannel();

    //3. 通过信道，创建交换机，队列，绑定信息
        //map对象
    google::protobuf::Map<std::string,std::string> tmp_map;
        //创建交换机
    channel->declareExchange("exchange1",mq::ExchangeType::TOPIC,true,false,tmp_map);
        //创建队列1
    channel->declareQueue("queue1",true,false,false,tmp_map);
        //创建队列2
    channel->declareQueue("queue2",true,false,false,tmp_map);
        //将 交换机1 和 队列1 / 2 绑定，并设置binding_Key
    channel->queueBind("exchange1","queue1","queue1");
    channel->queueBind("exchange1","queue2","news.music.#");

    // DLOG("设备构建完成!");
    //4. 订阅消息队列（创建消费者，并且确定接收的队列，接收消息）
        //因为需要多个channel函数，所以 使用函数适配器，提前设置参数
    auto functor = std::bind(&cb,channel,std::placeholders::_1,std::placeholders::_2,std::placeholders::_3);
    channel->basicConsume("consume",argv[1],false,functor);

    while(1) std::this_thread::sleep_for(std::chrono::seconds(3));//循环等待处理

    conn->closeChannel(channel);
    return 0;
}